package com.example.yuanqin.page.home

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.*
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.*
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.example.yuanqin.R
import com.example.yuanqin.TAG

sealed class HomeFragmentEvent(){
    object JumpAddressPage:HomeFragmentEvent()
}

val startPadding = 25.dp

@Preview
@Composable
fun HomeFragmentScreenPre(){
    MaterialTheme {
        HomeFragmentScreen(onEvent = { event ->
            Log.e(TAG, "HomeFragmentScreenPre: ", )
        })
    }
}

@Composable
fun HomeFragmentScreen(onEvent:(HomeFragmentEvent)->Unit){
    Column(modifier = Modifier
        .fillMaxHeight()
        .fillMaxWidth()
        .background(Color.White),
    ) {
        HomePageTopBar()
        HomePageSlideshow()
        NoticeLayout()
        SimpleDivider(isHorizontal = true)
        HomePageViewPage()
    }
}

@Composable
fun HomePageViewPage() {

}

@Composable
fun HomePageSlideshow() {
    Card(shape = RoundedCornerShape(4.dp)) {
        Image(painter = painterResource(id = R.mipmap.picture),
            contentDescription = "轮播图",
            modifier = Modifier.padding(startPadding,0.dp)
        )
    }
}

@Composable
private fun HomePageTopBar() {
    Row(modifier = Modifier
        .fillMaxWidth()
        .padding(0.dp, 15.dp),
        horizontalArrangement = Arrangement.Center,
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(painter = painterResource(id = R.mipmap.map),
            contentDescription = "定位图",
            modifier = Modifier
                .clickable {

                }
        )
        Spacer(modifier = Modifier.width(15.dp))

        SearchLayout(eidtable = false)
        Spacer(modifier = Modifier.width(15.dp))

        Text(text = "申请",fontSize = 17.sp,
            color = Color.Gray,
            modifier = Modifier.clickable {

            }
        )


    }

}

@Composable
private fun NoticeLayout(){
    Row(modifier = Modifier
        .fillMaxWidth()
        .padding(startPadding, 10.dp),
        verticalAlignment = Alignment.CenterVertically
    ) {
        Image(painter = painterResource(id = R.mipmap.ring), contentDescription = "通知",
            modifier = Modifier.padding(8.dp)
        )
        Spacer(modifier = Modifier.width(15.dp))
        Text(text = "刘老师的课程: 初二数学、物理课程即将报满",
            fontSize = 12.sp,
            color = Color.Red,
            overflow = TextOverflow.Ellipsis
        )
    }
}

@Composable
fun SimpleDivider(isHorizontal:Boolean){
    val modifier =
        if (isHorizontal){
            Modifier
                .height(1.dp)
                .fillMaxWidth()
        }else{
            Modifier
                .width(1.dp)
                .fillMaxHeight()
        }
    Divider(
        modifier = modifier,
        color = Color.LightGray,
    )
}

@Composable
fun SearchLayout(eidtable:Boolean){
    Card(shape = RoundedCornerShape(4.dp),
        contentColor = Color.DarkGray,) {
        Row(verticalAlignment = Alignment.CenterVertically,
            modifier = Modifier.size(235.dp,34.dp)
        ) {
            Image(painter = painterResource(id = R.mipmap.search),
                contentDescription = "搜索图标",
                modifier = Modifier.padding(18.dp,0.dp)
            )
            if (!eidtable){
                Text(text = "输入年纪、科目",
                    fontSize = 13.sp,
                    color = Color.DarkGray,)
            }
        }

        
    }
}
